package ch.uzh.ifi.seal.ssqa.popup.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import ch.uzh.ifi.seal.ssqa.ui.views.ArchitectureAnalysisView;

public class LoadAnalysisArchitectureAction implements IObjectActionDelegate {
	
	
	Shell shell;
	/**
	 * Constructor for Action1.
	 */
	public LoadAnalysisArchitectureAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		IWorkbenchWindow window =  PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 
		TreeSelection selection = (TreeSelection)window.getSelectionService().getSelection();
		if(selection.size() == 1){
			IFile f = (IFile)selection.getFirstElement();
			ArchitectureAnalysisView view = (ArchitectureAnalysisView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(ArchitectureAnalysisView.ID);
			view.setResourceURI(f.getFullPath().toString());
			view.loadViewDiagram();
		} else {
			ErrorDialog.openError(this.shell, "Resource not found", "The resource have selected can not be found or you have selected more than only one resource!", null);
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
